﻿

#region Using Statements

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Threading;
using System.ComponentModel;
using System.Reflection;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Design;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using Freeform;
using FGame = Freeform.Game;
using XGame = Microsoft.Xna.Framework.Game;
using FComponent = Freeform.Component;
using FViewport = Freeform.Viewport;

#endregion


namespace Freeform
{
    /// <summary>
    /// The MenuItem class represents a simple class for displaying 2D Text based menus in-game with ease.
    /// </summary>
    public class MenuItem : Entity2D
    {

        #region Events

        /// <summary>
        /// This event is fired when a mouse moves off of the MenuItem.
        /// </summary>
        public event MenuEventHandler ItemLeft;


        /// <summary>
        /// This event is fired when a mouse moves over the MenuItem.
        /// </summary>
        public event MenuEventHandler ItemHighlighted;


        /// <summary>
        /// This event is fired when the user clicks on the MenuItem.
        /// </summary>
        public event MenuEventHandler ItemClicked;


        /// <summary>
        /// This event is fired when the user right clicks on the MenuItem.
        /// </summary>
        public event MenuEventHandler ItemRightClicked;
        public delegate void MenuEventHandler(MenuItem item);

        #endregion


        #region Members

        /// <summary>
        /// Gets a reference to the Actor2D_Text used to render this item.
        /// </summary>
        public Actor2D_Text TextActor { get; private set; }


        /// <summary>
        /// Gets whether or not the mouse is hovering over this item.
        /// </summary>
        public Boolean IsHighlighted { get; private set; }

        #endregion


        #region Initialisation

        /// <summary>
        /// Creates a new MenuItem.
        /// </summary>
        /// <param name="World">The world to which the item belongs.</param>
        /// <param name="Text">The text to display on the item.</param>
        /// <param name="Font">The font for the text.</param>
        public MenuItem(World World, String Text, String Font)
            : base(World)
        {
            TextActor = new Actor2D_Text(this);
            TextActor.Text = Text;
            TextActor.Font = Game.Content.Load<SpriteFont>(Font);
            TextActor.UseNaturalFontSize = true;
            IsHighlighted = false;
        }

        #endregion


        #region Update

        /// <summary>
        /// Allows the MenuItem to update itself.
        /// </summary>
        /// <param name="time"></param>
        public override void Update(GameTime time)
        {
            base.Update(time);

            if (IsMouseOver)
            {
                // Fire ItemHighlighted event.
                if (!IsHighlighted && ItemHighlighted != null)
                    ItemHighlighted(this);
                IsHighlighted = true;

                // Fire ItemClicked event.
                if (Game.Input.MouseState_Current.LeftButton == ButtonState.Pressed &&
                    Game.Input.MouseState_Previous.LeftButton == ButtonState.Released &&
                    ItemClicked != null)
                    ItemClicked(this);

                // Fire ItemRightClicked event.
                if (Game.Input.MouseState_Current.RightButton == ButtonState.Pressed &&
                    Game.Input.MouseState_Previous.RightButton == ButtonState.Released &&
                    ItemRightClicked != null)
                    ItemRightClicked(this);
            }
            else
            {
                // Fire ItemLeft event.
                if (IsHighlighted && ItemLeft != null)
                    ItemLeft(this);
                IsHighlighted = false;
            }
        }

        #endregion

    }
}